package com.aruba.daggerapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.aruba.daggerapplication.api.BaiduApiService
import com.aruba.daggerapplication.di.ApplicationComponent
import com.aruba.daggerapplication.di.DaggerApplicationComponent
import com.aruba.daggerapplication.di.datasource.DataSource
import com.aruba.daggerapplication.di.datasource.RemoteDataSource
import com.aruba.daggerapplication.di.singleton.SingletonTest
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject

class MainActivity : AppCompatActivity() {

    /**
     * 表示该对象需要自动注入
     */
    @Inject
    lateinit var dataSource1: DataSource

    @Inject
    lateinit var dataSource2: DataSource

    /**
     * BaiduApiService注入
     */
    @Inject
    lateinit var baiduApiService: BaiduApiService

    @Inject
    lateinit var singleton: SingletonTest

    override fun onCreate(savedInstanceState: Bundle?) {
//        DaggerApplicationComponent.create().inject(this)
        Component.daggerApplicationComponent.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        Log.i("aruba_log", dataSource1.toString())
//        Log.i("aruba_log", dataSource2.toString())
//        //判断是否是一个对象
//        Log.i("aruba_log", "${dataSource2 === dataSource1}")
//
        Log.i("aruba_log", "MainActivity singleton hashcode: ${singleton.hashCode()}")

        getIndex()
        findViewById<TextView>(R.id.tv_hello).setOnClickListener {
//            startActivity(Intent(this@MainActivity, ScopeActivity::class.java))
            startActivity(Intent(this@MainActivity, SubActivity::class.java))
        }
    }

    /**
     * 获取百度首页
     */
    private fun getIndex() {
        baiduApiService.index().enqueue(object : Callback<String> {
            override fun onResponse(call: Call<String>, response: Response<String>) {
                findViewById<TextView>(R.id.tv_hello).text = response.body()
            }

            override fun onFailure(call: Call<String>, t: Throwable) {
            }
        })
    }
}